package com.cdu.ai_intelligent_teaching_and_training_system.service;

import com.cdu.ai_intelligent_teaching_and_training_system.entity.Exam;
import com.cdu.ai_intelligent_teaching_and_training_system.mapper.ExamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class ExamService {
    
    @Autowired
    private ExamMapper examMapper;
    
    public List<Exam> findAll() {
        return examMapper.selectAll();
    }
    
    public Optional<Exam> findById(Long id) {
        return Optional.ofNullable(examMapper.selectById(id));
    }
    
    public List<Exam> findByCourseId(Long courseId) {
        return examMapper.selectByCourseId(courseId);
    }
    
    // 需要在Mapper.xml中自定义即将到来的考试和模糊查询
    public List<Exam> findUpcomingExams() {
        return examMapper.selectByExamDateAfter(LocalDateTime.now());
    }
    
    public List<Exam> findByTitleContaining(String title) {
        return examMapper.selectByTitleLike("%" + title + "%");
    }
    
    public Exam save(Exam exam) {
        if (exam.getExamId() == null) {
            examMapper.insert(exam);
        } else {
            examMapper.update(exam);
        }
        return exam;
    }
    
    public void deleteById(Long id) {
        examMapper.deleteById(id);
    }
} 